package com.itheima.appserver.controller;

import com.itheima.appserver.service.ManagerUserService;
import com.itheima.commons.model.vo.MangerUserVo;
import com.itheima.commons.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户管理
 */
@RestController
@RequestMapping("management/manage")
public class ManagerUserController {

    @Autowired
    private ManagerUserService managerUserService;

    /**
     * 用户基本资料
     *
     * @return
     */
    @GetMapping("users/{userID}")
    public ResponseEntity queryUserInfo(@PathVariable("userID") Long userId) {
        MangerUserVo mangerUserVo = managerUserService.queryUserInfo(userId);
        return ResponseEntity.ok(mangerUserVo);
    }

    /**
     * 冻结用户
     *
     * @param params
     * @return
     */
    @PostMapping("users/freeze")
    public ResponseEntity freezeUsers(@RequestBody Map<String, String> params) {
        String userId = params.get("userId");
        String freezingTime = params.get("freezingTime");
        String freezingRange = params.get("freezingRange");
        String reasonsForFreezing = params.get("reasonsForFreezing");
        String frozenRemarks = params.get("frozenRemarks");
        String message = managerUserService.freezeUsers(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks);
        return ResponseEntity.ok(message);
    }

    /**
     * 解冻用户
     *
     * @param params
     * @return
     */
    @PostMapping("users/unfreeze")
    public ResponseEntity unfreezeUsers(@RequestBody Map<String,String> params) {
        String userId = params.get("userId");
        String reasonsForThawing = params.get("frozenRemarks");
        String message = managerUserService.unfreezeUsers(userId, reasonsForThawing);
        return ResponseEntity.ok(message);
    }

    /**
     * 用户数据翻页接口
     * @param page
     * @param pagesize
     * @param id
     * @param nickname
     * @param city
     * @return
     */
    @GetMapping("users")
    public ResponseEntity queryManagerUserByPage(@RequestParam Integer page,
                                                 @RequestParam Integer pagesize,
                                                 @RequestParam Long id,
                                                 @RequestParam String nickname,
                                                 @RequestParam String city) {
        /*if (ObjectUtil.isNull(id)) {
            id = null;
        }
        if (ObjectUtil.isNull(nickname)){
            nickname="";
        }
        if (ObjectUtil.isNull(city)){
            city="";
        }*/
        PageResult pageResult = managerUserService.queryManagerUserByPage(page, pagesize, id, nickname, city);
        return ResponseEntity.ok(pageResult);
    }
}
